package com.ca.ridersclubapplication.util;

import android.content.Context;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.ca.ridersclubapplication.R;

import jp.wasabeef.glide.transformations.BlurTransformation;

public class ImageUtils {

    public static void loadOverrideImage(Context context, String imageUrl, ImageView view) {
        try {
            Glide.with(context)
                    .load(imageUrl)
                    .override(8)
                    .transition(DrawableTransitionOptions.withCrossFade())
                    .error(R.drawable.ic_error_image)
                    .skipMemoryCache(true)//跳过内存缓存
                    .diskCacheStrategy(DiskCacheStrategy.NONE)//不缓冲disk硬盘中
                    .into(view);
        }
        catch (Exception ignore) {}
    }

    public static void loadImage(Context context, String imageUrl, ImageView view) {
        try {
            Glide.with(context)
                    .load(imageUrl)
                    .transition(DrawableTransitionOptions.withCrossFade())
                    .error(R.drawable.ic_error_image)
                    .skipMemoryCache(true)//跳过内存缓存
                    .diskCacheStrategy(DiskCacheStrategy.NONE)//不缓冲disk硬盘中
                    .into(view);
        }
        catch (Exception ignore) {}
    }

    public static void loadCircleImage(Context context, String imageUrl, ImageView view) {
        BitmapTransformation transformation = new CircleCrop();
        loadImage(context, imageUrl, view, transformation);
    }

    public static void loadRoundImage(Context context, String imageUrl, ImageView view) {
        loadRoundImage(context, imageUrl, view, 20);
    }

    public static void loadRoundImage(Context context, String imageUrl, ImageView view, int radius) {
        BitmapTransformation transformation = new RoundedCorners(radius);
        loadImage(context, imageUrl, view, transformation);
    }

    private static void loadImage(Context context, String imageUrl, ImageView view, BitmapTransformation transformation) {
        try {
            Glide.with(context)
                    .load(imageUrl)
//                .listener(new RequestListener<Drawable>() {
//                    @Override
//                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
//                        return false;
//                    }
//
//                    @Override
//                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
//
//                        FileOutputStream out = null;
//                        try {
//                            File file = File.createTempFile(imageUrl, null, context.getCacheDir());
//                            out = new FileOutputStream(file);
//                            ((BitmapDrawable) resource).getBitmap().compress(Bitmap.CompressFormat.JPEG, 100, out);
//                            out.close();
//                        }
//                        catch (Exception e) {
//                            Log.e("CacheImage Failed", e.getMessage());
//                        }
//                        finally {
//                            if (out != null) {
//                                try {
//                                    out.close();
//                                } catch (IOException e) {
//                                    e.printStackTrace();
//                                }
//                            }
//                        }
//
//                        return false;
//                    }
//                })
                    .transition(DrawableTransitionOptions.withCrossFade())
                    .error(R.drawable.ic_error_image)
                    .transform(transformation)
                    .skipMemoryCache(true)//跳过内存缓存
                    .diskCacheStrategy(DiskCacheStrategy.NONE)//不缓冲disk硬盘中
                    .into(view);
        }
        catch (Exception ignore) {}
    }

    public static void loadFuzzyImage(Context context, String imageUrl, ImageView imageView, int fuzzy) {
        try {
            Glide.with(context)
                    .load(imageUrl)
//                .listener(new RequestListener<Drawable>() {
//                    @Override
//                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
//                        return false;
//                    }
//
//                    @Override
//                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
//
//                        FileOutputStream out = null;
//                        try {
//                            File file = File.createTempFile(imageUrl, null, context.getCacheDir());
//                            out = new FileOutputStream(file);
//                            ((BitmapDrawable) resource).getBitmap().compress(Bitmap.CompressFormat.JPEG, 100, out);
//                            out.close();
//                        }
//                        catch (Exception e) {
//                            Log.e("CacheImage Failed", e.getMessage());
//                        }
//                        finally {
//                            if (out != null) {
//                                try {
//                                    out.close();
//                                } catch (IOException e) {
//                                    e.printStackTrace();
//                                }
//                            }
//                        }
//
//                        return false;
//                    }
//                })
                    .transition(DrawableTransitionOptions.withCrossFade())
                    .error(R.drawable.ic_error_image)
                    .apply(RequestOptions.bitmapTransform(new BlurTransformation(25, fuzzy)))
                    .skipMemoryCache(true)//跳过内存缓存
                    .diskCacheStrategy(DiskCacheStrategy.NONE)//不缓冲disk硬盘中
                    .into(imageView);
        }
        catch (Exception ignore) {}
    }
}
